﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【部门】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto部门
    /// </summary>
    public abstract class AutoDepartmentMapping : BaseMapping<Department, DepartmentModel, DepartmentFilterModel>
    {
        public AutoDepartmentMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override DepartmentModel Entity2Model(Department entity)
        {
            DepartmentModel model = new DepartmentModel();

            model.DepartmentID = entity.DepartmentID;
            model.GroupInfoID = entity.GroupInfoID;
            model.LeaderID = entity.LeaderID;
            model.LeaderName = entity.LeaderName;
            model.Name = entity.Name;
            model.Type = entity.Type;
            model.ProvinceID = entity.ProvinceID;
            model.CityID = entity.CityID;
            model.AreaID = entity.AreaID;
            model.ParentID = entity.ParentID;
            model.IsDelete = entity.IsDelete;
            model.CreateUserID = entity.CreateUserID;
            model.CreateUserName = entity.CreateUserName;
            model.CreateDate = entity.CreateDate;
            model.UpdateUserID = entity.UpdateUserID;
            model.UpdateUserName = entity.UpdateUserName;
            model.UpdateDate = entity.UpdateDate;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Department Model2Entity(DepartmentModel model)
        {
            Department entity = new Department();

            entity.DepartmentID = model.DepartmentID;
            entity.GroupInfoID = model.GroupInfoID;
            entity.LeaderID = model.LeaderID;
            entity.LeaderName = model.LeaderName;
            entity.Name = model.Name;
            entity.Type = model.Type;
            entity.ProvinceID = model.ProvinceID;
            entity.CityID = model.CityID;
            entity.AreaID = model.AreaID;
            entity.ParentID = model.ParentID;
            entity.IsDelete = model.IsDelete;
            entity.CreateUserID = model.CreateUserID;
            entity.CreateUserName = model.CreateUserName;
            entity.CreateDate = model.CreateDate;
            entity.UpdateUserID = model.UpdateUserID;
            entity.UpdateUserName = model.UpdateUserName;
            entity.UpdateDate = model.UpdateDate;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Department> FilterModel2Where(DepartmentFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._DepartmentID.HasValue)
            {
                where = where.And(x => x.DepartmentID == filterModel._DepartmentID.Value);
            }
            if (filterModel._DepartmentIDs.IsNotEmpty() && filterModel._DepartmentIDs.Count > 0)
            {
                where = where.And(x => filterModel._DepartmentIDs.Contains(x.DepartmentID));
            }
            if (filterModel._NotDepartmentIDs.IsNotEmpty() && filterModel._NotDepartmentIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotDepartmentIDs.Contains(x.DepartmentID));
            }

            //集团
            if (filterModel._GroupInfoID.HasValue)
            {
                where = where.And(x => x.GroupInfoID == filterModel._GroupInfoID.Value);
            }
            if (filterModel._GroupInfoIDs.IsNotEmpty() && filterModel._GroupInfoIDs.Count > 0)
            {
                where = where.And(x => filterModel._GroupInfoIDs.Contains(x.GroupInfoID));
            }
            if (filterModel._NotGroupInfoIDs.IsNotEmpty() && filterModel._NotGroupInfoIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotGroupInfoIDs.Contains(x.GroupInfoID));
            }

            //负责人
            if (filterModel._LeaderID.HasValue)
            {
                where = where.And(x => x.LeaderID == filterModel._LeaderID.Value);
            }
            if (filterModel._LeaderIDs.IsNotEmpty() && filterModel._LeaderIDs.Count > 0)
            {
                where = where.And(x => x.LeaderID.HasValue && filterModel._LeaderIDs.Contains(x.LeaderID.Value));
            }
            if (filterModel._NotLeaderIDs.IsNotEmpty() && filterModel._NotLeaderIDs.Count > 0)
            {
                where = where.And(x => x.LeaderID.HasValue && !filterModel._NotLeaderIDs.Contains(x.LeaderID.Value));
            }

            //负责人名称
            if (!string.IsNullOrEmpty(filterModel._LeaderName))
            {
                where = where.And(x => x.LeaderName.Contains(filterModel._LeaderName));
            }
            if (!string.IsNullOrEmpty(filterModel._LeaderNameEqual))
            {
                where = where.And(x => x.LeaderName == filterModel._LeaderNameEqual);
            }

            //名称
            if (!string.IsNullOrEmpty(filterModel._Name))
            {
                where = where.And(x => x.Name.Contains(filterModel._Name));
            }
            if (!string.IsNullOrEmpty(filterModel._NameEqual))
            {
                where = where.And(x => x.Name == filterModel._NameEqual);
            }

            //类型
            if (filterModel._Type.HasValue)
            {
                where = where.And(x => x.Type == filterModel._Type.Value);
            }
            if (filterModel._Types.IsNotEmpty() && filterModel._Types.Count > 0)
            {
                where = where.And(x => filterModel._Types.Contains(x.Type));
            }
            if (filterModel._NotTypes.IsNotEmpty() && filterModel._NotTypes.Count > 0)
            {
                where = where.And(x => !filterModel._NotTypes.Contains(x.Type));
            }

            //省
            if (filterModel._ProvinceID.HasValue)
            {
                where = where.And(x => x.ProvinceID == filterModel._ProvinceID.Value);
            }
            if (filterModel._ProvinceIDs.IsNotEmpty() && filterModel._ProvinceIDs.Count > 0)
            {
                where = where.And(x => x.ProvinceID.HasValue && filterModel._ProvinceIDs.Contains(x.ProvinceID.Value));
            }
            if (filterModel._NotProvinceIDs.IsNotEmpty() && filterModel._NotProvinceIDs.Count > 0)
            {
                where = where.And(x => x.ProvinceID.HasValue && !filterModel._NotProvinceIDs.Contains(x.ProvinceID.Value));
            }

            //市
            if (filterModel._CityID.HasValue)
            {
                where = where.And(x => x.CityID == filterModel._CityID.Value);
            }
            if (filterModel._CityIDs.IsNotEmpty() && filterModel._CityIDs.Count > 0)
            {
                where = where.And(x => x.CityID.HasValue && filterModel._CityIDs.Contains(x.CityID.Value));
            }
            if (filterModel._NotCityIDs.IsNotEmpty() && filterModel._NotCityIDs.Count > 0)
            {
                where = where.And(x => x.CityID.HasValue && !filterModel._NotCityIDs.Contains(x.CityID.Value));
            }

            //县
            if (filterModel._AreaID.HasValue)
            {
                where = where.And(x => x.AreaID == filterModel._AreaID.Value);
            }
            if (filterModel._AreaIDs.IsNotEmpty() && filterModel._AreaIDs.Count > 0)
            {
                where = where.And(x => x.AreaID.HasValue && filterModel._AreaIDs.Contains(x.AreaID.Value));
            }
            if (filterModel._NotAreaIDs.IsNotEmpty() && filterModel._NotAreaIDs.Count > 0)
            {
                where = where.And(x => x.AreaID.HasValue && !filterModel._NotAreaIDs.Contains(x.AreaID.Value));
            }

            //是否删除
            if (filterModel._IsDelete.HasValue)
            {
                where = where.And(x => x.IsDelete == filterModel._IsDelete.Value);
            }
            if (filterModel._IsDeletes.IsNotEmpty() && filterModel._IsDeletes.Count > 0)
            {
                where = where.And(x => filterModel._IsDeletes.Contains(x.IsDelete));
            }
            
            //创建人姓名
            if (!string.IsNullOrEmpty(filterModel._CreateUserName))
            {
                where = where.And(x => x.CreateUserName.Contains(filterModel._CreateUserName));
            }
            if (!string.IsNullOrEmpty(filterModel._CreateUserNameEqual))
            {
                where = where.And(x => x.CreateUserName == filterModel._CreateUserNameEqual);
            }

            //创建时间
            if (filterModel._CreateDate.HasValue)
            {
                where = where.And(x => x.CreateDate == filterModel._CreateDate.Value);
            }
            if (filterModel._CreateDate_Begin.HasValue)
            {
                where = where.And(x => x.CreateDate >= filterModel._CreateDate_Begin.Value);
            }
            if (filterModel._CreateDate_End.HasValue)
            {
                where = where.And(x => x.CreateDate < filterModel._CreateDate_End.Value.AddDays(1));
            }

            //修改人姓名
            if (!string.IsNullOrEmpty(filterModel._UpdateUserName))
            {
                where = where.And(x => x.UpdateUserName.Contains(filterModel._UpdateUserName));
            }
            if (!string.IsNullOrEmpty(filterModel._UpdateUserNameEqual))
            {
                where = where.And(x => x.UpdateUserName == filterModel._UpdateUserNameEqual);
            }

            //修改时间
            if (filterModel._UpdateDate.HasValue)
            {
                where = where.And(x => x.UpdateDate == filterModel._UpdateDate.Value);
            }
            if (filterModel._UpdateDate_Begin.HasValue)
            {
                where = where.And(x => x.UpdateDate >= filterModel._UpdateDate_Begin.Value);
            }
            if (filterModel._UpdateDate_End.HasValue)
            {
                where = where.And(x => x.UpdateDate < filterModel._UpdateDate_End.Value.AddDays(1));
            }

            return where;
        }
        #endregion
    }
}

